<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as Session;
use Feima\FlightLogProxy\FlightMonitorLogProxy as FlightMonitorLogProxy;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
}, true);

/**
 * Setting up the view component
 */
$di->set('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_',
                "compiledExtension" => ".compiled",
                'compileAlways'=>true,
                'autoescape'=>true

            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
}, true);

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('dbQuery', function () use ($config) {
    $conn = new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        "charset" => $config->database->charset,
        "port"=>$config->database->port
    ));
    return $conn;
});

$di->setShared('dbExecute', function () use ($config) {
    $conn = new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        "charset" => $config->database->charset,
        "port"=>$config->database->port
    ));
    return $conn;
});

//后台数据库
$di->setShared('dbFlightLogProxy', function() use ($config){
    $conn = new FlightMonitorLogProxy($config->flightLogProxyConfig);
    return $conn;
});

//Start the session the first time when some component request the session service
$di->setShared('session', function() {
    $session = new Session();
    $session->start();
    return $session;
});

$di->setShared('redis', function() use ($config){
    $redis = new Redis();
    $redis->connect($config->redis->host, $config->redis->port);
    $redis->auth($config->redis->password);
    return $redis;
});

$di->setShared('common', function() use ($config){
    $common = new Common();
    return $common;
});

$di->setShared('package',function(){
    $package = new Package();
    return $package;
});

$di->setShared('sqlite', function() use ($config){
    $sqlite = new SQLitePDO(md5 (uniqid(mt_rand(), true)));
    return $sqlite;
});

$di->setShared('rsa', function() use ($config){
    return $config['rsa'];
});

$di->setShared('host', function() use ($config){
    return $config['host'];
});

$di->setShared('database', function() use($config){
    return $config->database;
});
    
$di->setShared('yunpian', function() use ($config){
    return $config['yunpian'];
});
    
$di->setShared('Qcloud', function() use ($config){
    return $config['Qcloud'];
});

$di->setShared('filesCos', function() use($config){
    return $config['filesCos'];
});
$di->setShared('wechatDev', function() use($config){
    return $config['wechatDev'];
});
$di->setShared('iconCos', function() use($config){
    return $config['iconCos'];
});
    
$di->setShared('redisExpire', function() use ($config){
    return $config['redisExpire'];
});

$di->setShared('defaultAuthMenu', function() use ($config){
    return $config['default_user_auth_menu'];
});


$di->set('viewCache',function(){
    $frontEnd = new \Phalcon\Cache\Frontend\Output();
    $backEnd = new \Phaclon\Cache\Backend\File($frontEnd,array('cacheDir'=>$config->application->cacheDir ));
    return $backEnd;
});
$di->set('element', function () {
    return new Elements();
});



//$di->setShared('tcpdf', function() use ($config){
//    $tcpdf = new TCPDF();
//    return $tcpdf;
//});


